import {Injectable} from "@angular/core";
import {BehaviorSubject, Subject} from "rxjs";
import {Dialog} from "./dialog";

@Injectable()
export class OneDialogService {

  constructor() { }

  dialogSubject : Subject<any> = new BehaviorSubject<Dialog>(null);

  /**
   * 推送dialog
   * @param dialog
   */
  push(dialog:Dialog){
    this.dialogSubject.next(dialog);
    this.dialogSubject.next(null);
  }

  /**
   * 不带取消按钮的信息dialog
   * @param content
   * @param okFun
   * @param cancelFun
   * @param closeFun
   */
  info(content:any,okFun?:(this:void,...params:string[]) => void,cancelFun?:(this:void,...params:string[]) => void,closeFun?:(this:void,...params:string[]) => void ){
    this.push(this.dialog(Dialog.DialogType.Info,content,okFun,cancelFun,closeFun));
  }

  /**
   * 带取消按钮的信息dialog
   * @param content
   * @param okFun
   * @param cancelFun
   * @param closeFun
   */
  infoWithCancelBtn(content:any,okFun?:(this:void,...params:string[]) => void,cancelFun?:(this:void,...params:string[]) => void,closeFun?:(this:void,...params:string[]) => void ){
    this.push(this.dialogWithCancelBtn(Dialog.DialogType.Info,content,okFun,cancelFun,closeFun));
  }
  success(content:any,okFun?:(this:void,...params:string[]) => void,cancelFun?:(this:void,...params:string[]) => void,closeFun?:(this:void,...params:string[]) => void  ){
    this.push(this.dialog(Dialog.DialogType.Success,content,okFun,cancelFun,closeFun));
  }
  successWithCancelBtn(content:any,okFun?:(this:void,...params:string[]) => void,cancelFun?:(this:void,...params:string[]) => void,closeFun?:(this:void,...params:string[]) => void  ){
    this.push(this.dialogWithCancelBtn(Dialog.DialogType.Success,content,okFun,cancelFun,closeFun));
  }
  question(content:any,okFun?:(this:void,...params:string[]) => void,cancelFun?:(this:void,...params:string[]) => void,closeFun?:(this:void,...params:string[]) => void ){
    this.push(this.dialog(Dialog.DialogType.Question,content,okFun,cancelFun,closeFun));
  }
  questionWithCancelBtn(content:any,okFun?:(this:void,...params:string[]) => void,cancelFun?:(this:void,...params:string[]) => void,closeFun?:(this:void,...params:string[]) => void ){
    this.push(this.dialogWithCancelBtn(Dialog.DialogType.Question,content,okFun,cancelFun,closeFun));
  }

  warn(content:any,okFun?:(this:void,...params:string[]) => void,cancelFun?:(this:void,...params:string[]) => void,closeFun?:(this:void,...params:string[]) => void ){
    this.push(this.dialog(Dialog.DialogType.Warn,content,okFun,cancelFun,closeFun));
  }
  warnWithCancelBtn(content:any,okFun?:(this:void,...params:string[]) => void,cancelFun?:(this:void,...params:string[]) => void,closeFun?:(this:void,...params:string[]) => void ){
    this.push(this.dialogWithCancelBtn(Dialog.DialogType.Warn,content,okFun,cancelFun,closeFun));
  }
  error(content:any,okFun?:(this:void,...params:string[]) => void,cancelFun?:(this:void,...params:string[]) => void,closeFun?:(this:void,...params:string[]) => void ){
    this.push(this.dialog(Dialog.DialogType.Error,content,okFun,cancelFun,closeFun));
  }

  modalInfo(content:any,okFun?:(this:void,...params:string[]) => void,cancelFun?:(this:void,...params:string[]) => void,closeFun?:(this:void,...params:string[]) => void ){
    this.push(this.modal(Dialog.DialogType.Info,content,okFun,cancelFun,closeFun));
  }
  modalInfoWithCancelBtn(content:any,okFun?:(this:void,...params:string[]) => void,cancelFun?:(this:void,...params:string[]) => void,closeFun?:(this:void,...params:string[]) => void ){
    this.push(this.modalWithCancelBtn(Dialog.DialogType.Info,content,okFun,cancelFun,closeFun));
  }

  modalSuccess(content:any,okFun?:(this:void,...params:string[]) => void,cancelFun?:(this:void,...params:string[]) => void,closeFun?:(this:void,...params:string[]) => void  ){
    this.push(this.modal(Dialog.DialogType.Success,content,okFun,cancelFun,closeFun));
  }
  modalSuccessWithCancelBtn(content:any,okFun?:(this:void,...params:string[]) => void,cancelFun?:(this:void,...params:string[]) => void,closeFun?:(this:void,...params:string[]) => void  ){
    this.push(this.modalWithCancelBtn(Dialog.DialogType.Success,content,okFun,cancelFun,closeFun));
  }
  modalQuestion(content:any,okFun?:(this:void,...params:string[]) => void,cancelFun?:(this:void,...params:string[]) => void,closeFun?:(this:void,...params:string[]) => void ){
    this.push(this.modal(Dialog.DialogType.Question,content,okFun,cancelFun,closeFun));
  }
  modalQuestionWithCancelBtn(content:any,okFun?:(this:void,...params:string[]) => void,cancelFun?:(this:void,...params:string[]) => void,closeFun?:(this:void,...params:string[]) => void ){
    this.push(this.modalWithCancelBtn(Dialog.DialogType.Question,content,okFun,cancelFun,closeFun));
  }
  modalWarn(content:any,okFun?:(this:void,...params:string[]) => void,cancelFun?:(this:void,...params:string[]) => void,closeFun?:(this:void,...params:string[]) => void ){
    this.push(this.modal(Dialog.DialogType.Warn,content,okFun,cancelFun,closeFun));
  }
  modalWarnWithCancelBtn(content:any,okFun?:(this:void,...params:string[]) => void,cancelFun?:(this:void,...params:string[]) => void,closeFun?:(this:void,...params:string[]) => void ){
    this.push(this.modalWithCancelBtn(Dialog.DialogType.Warn,content,okFun,cancelFun,closeFun));
  }
  modalError(content:any,okFun?:(this:void,...params:string[]) => void,cancelFun?:(this:void,...params:string[]) => void,closeFun?:(this:void,...params:string[]) => void ){
    this.push(this.modal(Dialog.DialogType.Error,content,okFun,cancelFun,closeFun));
  }

  /**
   * 无取消按钮的消息框
   * @param type
   * @param content
   * @param okFun
   * @param cancelFun
   * @param closeFun
   * @returns {Dialog}
   */
  private dialog(type:any,content:any,okFun?:(this:void,...params:string[]) => void,cancelFun?:(this:void,...params:string[]) => void,closeFun?:(this:void,...params:string[]) => void  ):Dialog{
    return this.getDialog(false,false,type,content,okFun,cancelFun,closeFun);
  }

  /**
   * 带消息按钮的消息框
   * @param type
   * @param content
   * @param okFun
   * @param cancelFun
   * @param closeFun
   * @returns {Dialog}
   */
  private dialogWithCancelBtn(type:any,content:any,okFun?:(this:void,...params:string[]) => void,cancelFun?:(this:void,...params:string[]) => void,closeFun?:(this:void,...params:string[]) => void  ):Dialog{
    return this.getDialog(true,false,type,content,okFun,cancelFun,closeFun);
  }

  /**
   * 无消息按钮的modal框
   * @param type
   * @param content
   * @param okFun
   * @param cancelFun
   * @param closeFun
   * @returns {Dialog}
   */
  private modal(type:any,content:any,okFun?:(this:void,...params:string[]) => void,cancelFun?:(this:void,...params:string[]) => void,closeFun?:(this:void,...params:string[]) => void ):Dialog{
    return this.getDialog(false,true,type,content,okFun,cancelFun,closeFun);
  }

  /**
   * 带消息按钮的modal框
   * @param type
   * @param content
   * @param okFun
   * @param cancelFun
   * @param closeFun
   * @returns {Dialog}
   */
  private modalWithCancelBtn(type:any,content:any,okFun?:(this:void,...params:string[]) => void,cancelFun?:(this:void,...params:string[]) => void,closeFun?:(this:void,...params:string[]) => void ):Dialog{
    return this.getDialog(true,true,type,content,okFun,cancelFun,closeFun);
  }

  private getDialog(showCancelBtn:any,isModal:any,type:any,content:any,okFun?:(this:void,...params:string[]) => void,cancelFun?:(this:void,...params:string[]) => void,closeFun?:(this:void,...params:string[]) => void ):Dialog{
    let dialog = new Dialog();
    dialog.showCancelBtn = !!showCancelBtn;
    dialog.isModal = isModal || false;
    dialog.type= type;
    dialog.content = content || "";
    dialog.closeFun = closeFun || cancelFun;
    dialog.cancelBtnFun = cancelFun || closeFun;
    dialog.okBtnFun = okFun;
    return dialog;
  }
}
